package com.zxblog.filter;

import com.zxblog.domain.UserAdmin;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(urlPatterns = {"/article/*","/user/*","/admin/*"},filterName = "userAuthenticationFilter")
public class UserAuthenticationFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
        if (httpServletRequest.getRequestURL().toString().contains("/article/")){
            // 文章拦截
            try {
                UserAdmin adminUser = (UserAdmin) httpServletRequest.getSession().getAttribute("admin");
                if (adminUser.getUid() != 0){
                    // 放行流量
                    filterChain.doFilter(servletRequest, servletResponse);
                }
            } catch (Exception e){
                // 拒绝流量,并转发到错误页面
                httpServletResponse.sendRedirect("/admin/login.html");
                //httpServletRequest.getRequestDispatcher("/admin/login.html").forward(servletRequest, servletResponse);
                //httpServletRequest.getRequestDispatcher("/adminLoginErrorPage.html").forward(servletRequest, servletResponse);
            }
        }
        if (httpServletRequest.getRequestURL().toString().contains("/user/")){
            // 用户拦截
            if (httpServletRequest.getRequestURL().toString().contains("/user/login")){

            } else {

            }
        }
        if (httpServletRequest.getRequestURL().toString().contains("/admin/")){
            // 管理员拦截
            if (httpServletRequest.getRequestURL().toString().contains("/admin/login")){
                // 判断是登录就放行
                filterChain.doFilter(servletRequest, servletResponse);
            } else {
                try {
                    UserAdmin adminUser = (UserAdmin) httpServletRequest.getSession().getAttribute("admin");
                    if (adminUser.getUid() != 0){
                        // 放行流量
                        filterChain.doFilter(servletRequest, servletResponse);
                    }
                } catch (Exception e){
                    // 拒绝流量,并转发到错误页面
                    httpServletResponse.sendRedirect("/admin/login.html");
                    //httpServletRequest.getRequestDispatcher("/admin/login.html").forward(servletRequest, servletResponse);
                    //httpServletRequest.getRequestDispatcher("/adminLoginErrorPage.html").forward(servletRequest, servletResponse);
                }
            }

        }
        // 放行以外所有流量
        // filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }
}
